package com.googlecode.gamedroid;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckedTextView;

import com.googlecode.gamedroid.gamepad.GamePad;

public class GamePadAdapter extends ArrayAdapter<Class<? extends GamePad>>
{
	private Class<? extends GamePad> activeItem = null;
	private LayoutInflater inflater;
	
	public GamePadAdapter(Context context)
	{
		super(context, 0);
		inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	
	public Class<? extends GamePad> getActiveItem()
	{
		return activeItem;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		Class<? extends GamePad> item = getItem(position);
		
		CheckedTextView ctv;
		ctv = (CheckedTextView) inflater.inflate(android.R.layout.simple_list_item_single_choice, parent, false);
		ctv.setChecked(item == activeItem);
		ctv.setText(GamePad.getLabel(item));
		
		Drawable res = getContext().getResources().getDrawable(GamePad.getIcon(item));
		ctv.setCompoundDrawablePadding(10);
		ctv.setCompoundDrawablesWithIntrinsicBounds(res, null, null, null);
		return ctv;
	}
	
	@Override
	public int getViewTypeCount()
	{
		return 1;
	}
	
	public void setActiveItem(Class<? extends GamePad> activeItem)
	{
		this.activeItem = activeItem;
		notifyDataSetInvalidated();
	}
}
